import cv2
import os
import glob
from skimage import exposure, morphology, color, img_as_bool
import numpy as np


def get_centerline(path):
    for file in glob.glob(os.path.join(path, '*.tif')):
        print(file)
        index = os.path.basename(file)[:-4]
        image = cv2.imread(file)

        image = img_as_bool(color.rgb2gray(image))
        skelton = morphology.skeletonize(image)

        save_name = "/home/imed/视频/3Djiaomo/results/centerline/" + index + '.tif'

        cv2.imwrite(save_name, skelton * 255)
        print(file, '\tdone!')


###  标签没有阈值化，所以fdr=1, sen=0像素点划分错误 12.01日
def thredhold():
    file_path = "/home/jiayu/MyProject_2022/CCM_90/gt3_before/"
    for file in glob.glob(os.path.join(file_path, "*")):
        img = cv2.imread(file, flags=1)
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        ret, thred = cv2.threshold(gray, 10, 255, cv2.THRESH_BINARY)
        cv2.imwrite(path, thred)


def convttif2png(path):
    for file in glob.glob(os.path.join(path, "*")):
        img = cv2.imread(file, flags=-1)
        cv2.imwrite(os.path.join("/home/jiayu/MyProject_2022/CCM_120/test/gt3", os.path.basename(file)[:-4] + ".png"),
                    img)


if __name__ == '__main__':
    path = '/home/jiayu/MyProject_2022/CCM_120/test/gt3(copy)/'  # '/home/imed/视频/Unet_jiaomo/results/jiaomo_dataset/transunet/bce/first_stage/Thresh/'
    # get_centerline(path)
    # thredhold()
    convttif2png(path)
